//=============================================================================
// DirectInput.cpp
//=============================================================================

#include "d3dUtil.h"
#include "DirectInput.h"
#include "DirectXFramework.h"


DirectInput* gDInput = 0;

DirectInput::DirectInput(DWORD keyboardCoopFlags, DWORD mouseCoopFlags)
{
	ZeroMemory(mKeyboardState, sizeof(mKeyboardState));
	ZeroMemory(&mMouseState, sizeof(mMouseState));
	ZeroMemory(&ControllerState, sizeof(ControllerState));

	XInputEnable(true);

	DirectInput8Create(gd3dApp->getAppInst(), DIRECTINPUT_VERSION, 
		IID_IDirectInput8, (void**)&mDInput, 0);

	mDInput->CreateDevice(GUID_SysKeyboard, &mKeyboard, 0);
	mKeyboard->SetDataFormat(&c_dfDIKeyboard);
	mKeyboard->SetCooperativeLevel(gd3dApp->getMainWnd(), keyboardCoopFlags);
	mKeyboard->Acquire();

	mDInput->CreateDevice(GUID_SysMouse, &mMouse, 0);
	mMouse->SetDataFormat(&c_dfDIMouse2);
	mMouse->SetCooperativeLevel(gd3dApp->getMainWnd(), mouseCoopFlags);
	mMouse->Acquire();

	for(short i = 0; i < 4; ++i)
	{
		ZeroMemory(&ControllerState[i], sizeof(XINPUT_STATE));
	}

	PlayerNumber[0] = 0;
	PlayerNumber[1] = 1;
	PlayerNumber[2] = 2;
	PlayerNumber[3] = 3;

}

void DirectInput::Vibrate(short PlayerID, int leftVal, int rightVal)
{

	if(PlayerID > 3 || PlayerID < 0)
		return;

	XINPUT_VIBRATION Vibration;
	
	ZeroMemory(&Vibration, sizeof(XINPUT_VIBRATION));

	Vibration.wLeftMotorSpeed = leftVal;
	Vibration.wRightMotorSpeed = rightVal;
	XInputSetState(PlayerID, &Vibration);
}



DirectInput::~DirectInput()
{
	XInputEnable(false);
	ReleaseCOM(mDInput);
	mKeyboard->Unacquire();
	mMouse->Unacquire();
	ReleaseCOM(mKeyboard);
	ReleaseCOM(mMouse);
}

void DirectInput::poll()
{
	// Poll keyboard.
	HRESULT hr = mKeyboard->GetDeviceState(sizeof(mKeyboardState), (void**)&mKeyboardState); 
	if (FAILED(hr))
	{
		// Keyboard lost, zero out keyboard data structure.
		ZeroMemory(mKeyboardState, sizeof(mKeyboardState));

		 // Try to acquire for next time we poll.
		hr = mKeyboard->Acquire();
	}

	// Poll mouse.
	hr = mMouse->GetDeviceState(sizeof(DIMOUSESTATE2), (void**)&mMouseState); 
	if (FAILED(hr))
	{
		// Mouse lost, zero out mouse data structure.
		ZeroMemory(&mMouseState, sizeof(mMouseState));

		// Try to acquire for next time we poll.
		hr = mMouse->Acquire(); 
	}

	DWORD Result;

	/*for(short i = 0; i < 4; ++i)
	{
		Result = XInputGetState(PlayerNumber[i], &ControllerState[i]);
		if(Result != ERROR_SUCCESS)
		{
			ZeroMemory(&ControllerState[i], sizeof(ControllerState));
		}
	}*/
	Result = XInputGetState(PlayerNumber[0], &ControllerState[0]);

}

bool DirectInput::keyDown(unsigned char key)
{
	return (mKeyboardState[key] & 0x80) != 0;
}

bool DirectInput::mouseButtonDown(int button)
{
	return (mMouseState.rgbButtons[button] & 0x80) != 0;
}

float DirectInput::mouseDX()
{
	return (float)mMouseState.lX;
}

float DirectInput::mouseDY()
{
	return (float)mMouseState.lY;
}

float DirectInput::mouseDZ()
{
	return (float)mMouseState.lZ;
}

XINPUT_STATE* DirectInput::GetControllerStatePointer(short PlayerID)
{
	if(PlayerID > 3 || PlayerID < 0)
	{
		return NULL;
	}

	return &ControllerState[PlayerID];
}